#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  07-banIcmp-lv4.sh
# Version	:  1.0
# Date		:  2020/05/14
# Author	:  Lz
# Email		:  lz843723683@gmail.com
# History	:     
#             Version 1.0, 2020/05/14
# Function	:   packages - 07.测试安全四级-禁止ICMP重定向
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

## TODO : 测试收尾清除工作
#
BANICMPClean_FTLV4(){
	return $TPASS
}


## TODO: 调用程序退出函数
#    In: $1 => 调用脚本结果值
BANICMPExit_FTLV4(){
        # 调用退出函数，其中调用了clean相关函数
	Exit_FTLIB ${1}
}


## TODO: 用户界面
#
BANICMPUSAGE_FTLV4(){
	USAGE_FTLIB "安全四级测试 - 禁止ICMP重定向"
}


## TODO : 测试前的初始化 
#  Out  : 
#         0 => TPASS
#         1 => TFAIL
#         other=> TCONF
BANICMPInit_FTLV4(){
        # 调用初始化函数
	Init_FTLIB "BANICMPClean_FTLV4"
        # 调用用户界面函数
 	BANICMPUSAGE_FTLV4
}


## TODO ：禁止ICMP重定向
#
BANICMPTest01_FTLV4(){
	local ret="$TPASS"

        local flag1="net.ipv4.conf.default.accept_redirects = 0"
        local flag2="net.ipv4.conf.all.accept_redirects = 0"
	local cmd="sysctl -p"

	eval $cmd | grep -q "$flag1"
	if [ $? -eq 0 ];then
	# 存在flag1对应字符串
		eval $cmd | grep -q "$flag2"		
		if [ $? -ne 0 ];then
			ret=$TFAIL
		fi
	else
		ret=$TFAIL
	fi

	echo "Command : $cmd"
	eval $cmd
	if [ $ret -eq $TFAIL ];then
		# 失败，未找到对应配置
		echo "未找到配置: $flag1 或 $flag2"
	fi

	OutputRet_FTLIB ${ret}
        RetParse_FTLIB "禁止ICMP重定向，存在${flag1} 和 ${flag2}" "False"
}


## TODO : Main
#  Out  : 
#         0 => TPASS
#         1 => TFAIL
#         other => TCONF
BANICMPMain_FTLV4(){
	BANICMPInit_FTLV4

	BANICMPTest01_FTLV4

	return ${TPASS}	
}

BANICMPMain_FTLV4
BANICMPExit_FTLV4 $?
